package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: talkin
 * Date: 14-5-28
 * Time: 下午4:47
 * To change this template use File | Settings | File Templates.
 */
public class Biblioteca {
    List<Book> books_biblioteca = new ArrayList<Book>();
    private int amount_book = 0;
    private int amount_movie = 0;
    public String bookName;
    public String movieName;

    User user = new User("jing", "jing@gmail.com", "1111");

    public String welcomeMessage() {
        String message = "Hello,world!";
        System.out.println(message);
        return message;
    }

    public String getListOfBooks(List<Book> books) {
        System.out.println("A list of all library books:");
        String result = "";
        amount_book = 0;
        for (Book book : books) {
            amount_book++;
            result += amount_book + "、" + book.getName() + "," + book.getAuthor() + "," + book.getYear() + "\n";
        }
        System.out.print(result);
        return result;
    }

    public void addBook(Book book, List<Book> books) {
        books.add(book);
        books_biblioteca.add(book);
        amount_book++;
    }

    public Book checkOutBook(String name, List<Book> books) {
        for (int i=0; i< amount_book; i++) {
            bookName = books.get(i).getName();
            if (bookName.equals(name)){
                Book result = books.get(i);
                books.remove(i);
                System.out.println("   ******   Thank you! Enjoy the book.   *******");
                return result;
            }
        }
        System.out.println("   ******   That book is not available.   *******");
        return null;
    }

    public Book returnBook(String bookName, List<Book> books) {
        boolean tag = true;
        for (Book book : books) {
            if (bookName.equals(book.getName())) {
                tag = false;
            }
        }
        if (tag) {
            for (Book book_view : books_biblioteca) {
                if (bookName.equals(book_view.getName())) {
                    addBook(book_view, books);
                    System.out.println("   ******   Thank you for returning the book.   *******");
                    return book_view;
                }
            }
        }
        System.out.println("   ******   That is not valid book to return.   *******");
        return null;
    }

    public void addMovie(Movie movie, List<Movie> movies) {
        movies.add(movie);
        amount_movie++;
    }

    public String getListOfMovies(List<Movie> movies) {
        System.out.println("A list of all library books:");
        String result = "";
        amount_movie = 0;
        for (Movie movie : movies) {
            amount_movie++;
            result += amount_movie + "、" + movie.getName() + "," + movie.getYear() + "," + movie.getDirector() + "," + movie.getRating() + "\n";
        }
        System.out.print(result);
        return result;
    }

    public Movie checkOutMovie(String name, List<Movie> movies) {
        for (int i=0; i<amount_movie; i++) {
            movieName = movies.get(i).getName();
            if (movieName.equals(name)){
                Movie result = movies.get(i);
                movies.remove(i);
                System.out.println("   ******   Thank you! Enjoy the movie.   *******");
                return result;
            }
        }
        System.out.println("   ******   That movie is not available.   *******");
        return null;

    }

    public Boolean logIn(String libraryNumber, String password) {
        if (libraryNumber != "" && password != "") {
            return true;
        }
        return false;
    }

    public String userInformation(User user) {
        String name = user.getName();
        String email = user.getEmail();
        String phoneNumber = user.getPhoneNumber();
        String result = "user name : " + name + "\nemail address : " + email + "\nphone number : " + phoneNumber;
        System.out.println(result);
        return result;
    }
}
